new Vue({
 el:'#main',
 data:{
    username: localStorage.getItem('userObj') && JSON.parse(localStorage.getItem('userObj')).checkAgree ?  JSON.parse(localStorage.getItem('userObj')).username:'',

    usernameText:{state:0,des:''},

    password:localStorage.getItem('userObj') && 
    JSON.parse(localStorage.getItem('userObj')).checkAgree ? JSON.parse(localStorage.getItem('userObj')).password:'',

    passwordText:{state:0,des:''},
    
    checkAgree: JSON.parse(localStorage.getItem('userObj')) ? JSON.parse(localStorage.getItem('userObj')).checkAgree : false,

    userObj:localStorage.getItem('userObj') ? JSON.parse(localStorage.getItem('userObj')) : null,
 },
 methods:{

   chcek_Agree(){
    this.checkAgree = !this.checkAgree;
   },

   toLogin(){
      if(!this.checkPhoneNo(this.username)) return this.usernameText = {state:1,des:'请输入正确的手机号呢'};
      else this.usernameText = {state:0,des:''};
      if(this.password.length<6||this.password.length>16) return this.passwordText = {state:1,des:'请输入正确的密码呢'}
      else this.passwordText = {state:0,des:''};
      this.$http.post('/moongoDB/checkLogin',{
        username:this.username,
        password:this.password
      }).then(resp=>{
        var res = resp.body;
        if(res.des.indexOf('手机')!= -1 && res.state == 1) return this.usernameText = res;
        if(res.des.indexOf('密码')!= -1 && res.state == 1) return this.passwordText = res;
        resp.body.obj.checkAgree = this.checkAgree;
        localStorage.setItem('userObj',JSON.stringify(resp.body.obj));
        localStorage.setItem('isLogin',true);
        noticeDiv(res.des,'success',2000,function(){ window.location.href = '/';});
      })
   },

   checkPhoneNo(o){
    var re=/^1[3456789]\d{9}$/;
    if(!re.test(o)){
      return false;
    }else{
      return true;
    }
  },

  clearError(prop){
    this[prop+'Text'] = {state:0,des:''};
  },

  checkUser(){
    if(this.userObj && localStorage.getItem('isLogin')) window.location.href = '/';
  },
   
   toRegister(){
    window.location.href='/register'
   }
  
 },

 created(){
   this.checkUser();
   //localStorage.removeItem('userObj')
 },
 updated(){   

 },
 
});